<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;


class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $headers = [
            'Access-Control-Allow-Origin'=>config('lboy.domain'),
            'Access-Control-Allow-Headers'=>'Origin, Content-Type, Cookie, Accept, Authorization, multipart/form-data, application/json',
            'Access-Control-Allow-Methods'=>'GET, DELETE, POST, PATCH, PUT, OPTIONS',
            'Access-Control-Allow-Credentials'=>'true',
        ];

        if($request->getMethod() == 'OPTIONS') return response('ok', 200,$headers);

        $response = $next($request);

        foreach ($headers as $k=>$v){
            $response->header($k, $v);
        }

        return $response;
    }
}
